package org.javaboy.zk;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class ZkDemo01 implements Watcher {

    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws IOException, InterruptedException {
        ZooKeeper zk = new ZooKeeper("s2.javaboy.org:2181", 5000, new ZkDemo01());
        //注意，这个时候其实并没有建立连接
        ZooKeeper.States state = zk.getState();
        System.out.println("state = " + state);
        //暂停，当countDownLatch减为0的时候，程序才会继续执行
        countDownLatch.await();
    }

    public void process(WatchedEvent watchedEvent) {
        if(watchedEvent.getState() == Event.KeeperState.SyncConnected) {
            System.out.println("连接成功");
            // -1
            countDownLatch.countDown();
        }
    }
}
